package com.glory.brepda.activity.recipe;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.RecipeListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.TempRecipeEntity;
import com.glory.brepda.entity.UpdateIPEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RecipeUpImageActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqpID)
    FreeUI_ClearEditText etEqpID;
    @BindView(R.id.list_tv1)
    TextView listTv1;
    @BindView(R.id.list_tv2)
    TextView listTv2;
    @BindView(R.id.list_tv3)
    TextView listTv3;
    @BindView(R.id.list_tv4)
    TextView listTv4;
    @BindView(R.id.rvRecipeList)
    RecyclerView rvRecipeList;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvEqpWorkType)
    TextView tvEqpWorkType;
    @BindView(R.id.tvProgramType)
    TextView tvProgramType;
    @BindView(R.id.tvProcessName)
    TextView tvProcessName;
    @BindView(R.id.eqpScan)
    ImageView eqpScan;
    private static final int REQUEST_CODE_TEXT1 = 1001;
    String PROCESSNAME = "";
    String EQPID = "";
    private RecipeListAdapter mAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_recipe_up_image;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvTitle.setText(title);
        tvBaseSubmit.setText("查询");
        listTv1.setText("设备编号");
        listTv2.setText("Recipe名称");
        listTv3.setText("Recipe版本");
        listTv4.setText("程序类型");
        initRecyclerView(rvRecipeList, this);
        mAdapter = new RecipeListAdapter(null);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mAdapter.isFirstOnly(true);
        rvRecipeList.setAdapter(mAdapter);
        initEdit(etEqpID, s -> {
            getWqpInfo(s);
        });

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("EQPID", mAdapter.getData().get(position).getEquipmentId());
                bundle.putString("RecipeName", mAdapter.getData().get(position).getRecipeName());
                bundle.putString("ID", mAdapter.getData().get(position).getId()+"");
                bundle.putString("TITLE", "焊点图片上传");
                FreeApi_Utils.skipWithExtraIntent("", bundle,RecipeUpImageActivity.this, SolderJointUpImageActivity.class);
            }
        });
    }

    private void getWqpInfo(String eqpID) {
        tvEqpWorkType.setText("");
        tvProgramType.setText("");
        tvProcessName.setText("");
        mAdapter.setNewData(null);
        FreeUI_EntityCallBack<BaseResponseBean<UpdateIPEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<UpdateIPEntity>>
                (new TypeToken<BaseResponseBean<UpdateIPEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<UpdateIPEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    UpdateIPEntity body = response.body().getResponse().getBody();
                    tvEqpWorkType.setText(body.getEQUIPMENTTYPE());
                    tvProgramType.setText(body.getPROGRAMTYPE());
                    tvProcessName.setText(body.getPROCESSNAME());
                    PROCESSNAME = body.getPROCESSNAME();
                    EQPID = eqpID;
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<UpdateIPEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "GetEquipment");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("EQUIPMENTID", eqpID);
        NetUtils.requestPostNet("IF.GetEquipment", "GetEquipment", RecipeUpImageActivity.this, map, callBack);
    }

    @Override
    protected void initData() {
    }

    @OnClick({R.id.tvBaseSubmit,R.id.eqpScan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                TempRecipeRequestPostNet();
                break;
            case R.id.eqpScan:
                startQrScanner(REQUEST_CODE_TEXT1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            String qrResult = data.getStringExtra("SCAN_RESULT"); // 获取扫码结果
            if (qrResult != null) {
                switch (requestCode) {
                    case REQUEST_CODE_TEXT1:
                        etEqpID.setText(qrResult);
                        break;
                }
            }

        }
    }

    private void TempRecipeRequestPostNet() {
        FreeUI_EntityCallBack<BaseResponseBean<TempRecipeEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<TempRecipeEntity>>
                (new TypeToken<BaseResponseBean<TempRecipeEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<TempRecipeEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    List<TempRecipeEntity.TempRecipeInfoEntity> list = response.body().getResponse().getBody().getList();
                    if (list.size() <= 0) {
                        showToast("暂无Recipe信息", false);
                        return;
                    }
                    mAdapter.setNewData(list);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<TempRecipeEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "getRecipeByStatus");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("total", 13);
        map.addProperty("pageNo", 1);
        map.addProperty("pageSize", 50);
        map.addProperty("sort", "createTime");
        map.addProperty("order", "descending");
        map.addProperty("equipmentId", EQPID);
        map.addProperty("status", -1);
        map.addProperty("procedure", PROCESSNAME);
        NetUtils.requestPostNet("IF.getRecipeByStatus", "getRecipeByStatus", RecipeUpImageActivity.this, map, callBack);

    }


}
